﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDotNet;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Data_ConverteR
{
    static class Utils
    {
        public static DataFrame ConvertData(REngine engine, string fName, int type)
        {   
            DataFrame nullDf = null;
            switch (type) {
                case 0:
                   engine.Evaluate("mydata <- read.table('"+ fName +"', header=TRUE, sep=',')");
                   DataFrame rdataCSV = engine.Evaluate("mydata").AsDataFrame();
                   return rdataCSV;
                case 1:
                   engine.Evaluate("mydata <- read.dta('" + fName + "')");
                   DataFrame rdataSTATA = engine.Evaluate("mydata").AsDataFrame();
                   return rdataSTATA;
                case 2:
                   engine.Evaluate("mydata <- read.spss('"+fName+"', to.data.frame=TRUE)");
                   DataFrame rdataSPSS = engine.Evaluate("mydata").AsDataFrame();
                   return rdataSPSS;
                case 3:
                   engine.Evaluate("mydata <- sasxport.get('" + fName + "')");
                    DataFrame rdataSAS = engine.Evaluate("mydata").AsDataFrame();
                   return rdataSAS;
                case 4:
                    engine.Evaluate("mydata <- read.systat('" + fName + "', to.dataframe='TRUE')");
                    DataFrame rdataSYST = engine.Evaluate("mydata").AsDataFrame();
                    return rdataSYST;
                case 5:
                    engine.Evaluate("mydata <- read.epiinfo('" + fName + "')");
                    DataFrame rdataEPI = engine.Evaluate("mydata").AsDataFrame();
                    return rdataEPI;
            }
            return nullDf;  
        }
        public static void WriteData(REngine engine, DataFrame mydata, int type, string folder,string fName ,Dictionary<string, int> Opt = null)
        {
            engine.SetSymbol("mydata", mydata);
            switch (type)
            {
                case 0:
                    string cmdCSV = @"write.csv(mydata, file = '{0}/{1}.csv')";
                    cmdCSV = String.Format(cmdCSV, new object[] { folder, fName });
                    engine.Evaluate(cmdCSV);
                    break;
                case 1:
                    string cmdSTATA = @"write.dta(mydata, '{0}/{1}.dta')";
                    cmdSTATA = String.Format(cmdSTATA, new object[]{folder, fName});
                    engine.Evaluate(cmdSTATA);
                    break;
                case 2:    
                    string cmdSPSS = @"write.foreign(mydata, '{0}/{1}.txt', '{2}/{3}.sps', package='SPSS')";
                    cmdSPSS = String.Format(cmdSPSS, new object[]{folder,fName,folder,fName});
                    engine.Evaluate(cmdSPSS);
                    break;
                case 3:
                    string cmdSAS = @"write.foreign(mydata, '{0}/{1}.txt', '{2}/{3}.sas', package='SAS')";
                    cmdSAS = String.Format(cmdSAS, new object[]{folder,fName,folder,fName});
                    engine.Evaluate(cmdSAS);
                    break;
            }
        }
        public static String Get45or451FromRegistry()
        {
            using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"))
            {
                int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));
                if (true)
                {
                    return ("Version: " + CheckFor45DotVersion(releaseKey));
                }
            }
        }

        public static String CheckFor45DotVersion (int releaseKey)
        {
            if ((releaseKey >= 379893)) {
		        return "4.5.2 or later";
	        }
	        if ((releaseKey >= 378675)) {
		        return "4.5.1 or later";
	        }
	        if ((releaseKey >= 378389)) {
		        return "4.5 or later";
	        }
            return "No 4.5 or later version detected";
        }
        public static String GetRHome()
        {
            string retval = Environment.GetEnvironmentVariable("R_HOME");
            if (retval==null){
                return @"R Path is not set. Click Button to choose R HOME Folder";
            }
            return retval;
        }
        public static String GetRPath()
        {
            string[] paths = Environment.GetEnvironmentVariable("Path").Split(';');
            string retval = null;
            foreach (string spath in paths)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(spath, @"\\R\\R-.*\\bin"))
                {
                    retval = spath;
                    return retval;
                }
                else {
                    retval = @"R Path is not set. Click Button to choose R Path Folder";
                }
                    
            }
            return retval;
        }
      
    }
}
